/* Created by Richard Fleming
 * Copyright 2008 Johnson County Community College
 * Free for non-commercial use
 */

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace XnaUtility.GameObjects
{
    public class Base3DGameObjectManager : DrawableGameComponent
    {
        #region "Properties and variables"
        LinkedList<Base3DGameObject> objectList;
        Game game;

        private int maxObjects;

        public int MaxObjects
        {
            get { return maxObjects; }
            set { maxObjects = value; }
        }
        #endregion

        #region "Constructors and Initializers"
            public Base3DGameObjectManager(Game game) : base(game)
            {
                this.game = game;
                maxObjects = 100;
                objectList = new LinkedList<Base3DGameObject>();
            }

        public override void Initialize()
        {
            base.Initialize();
        }

        #endregion

        #region "Methods"

        public override void Update(GameTime gameTime)
        {

            for (LinkedListNode<Base3DGameObject> curNode = objectList.First; curNode != null; curNode = curNode.Next)
            {
                if (curNode.Value.alive)
                {
                    curNode.Value.Update(gameTime);
                }
                else
                {
                    LinkedListNode<Base3DGameObject> nextNode = curNode.Next;
                    objectList.Remove(curNode);
                    curNode = null;
                    curNode = nextNode;
                    if (curNode == null) break;
                }

            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {

            game.GraphicsDevice.RenderState.DepthBufferEnable = true;
            game.GraphicsDevice.RenderState.AlphaBlendEnable = false;
            game.GraphicsDevice.RenderState.AlphaTestEnable = false;

            for (LinkedListNode<Base3DGameObject> curNode = objectList.First; curNode != null; curNode = curNode.Next)
            {
                if (curNode.Value.alive)
                {
                    curNode.Value.Draw(gameTime);
                }
            }

            base.Draw(gameTime);
        }

        public bool AddGameObject(Base3DGameObject obj)
        {
            bool result = true;

            if (objectList.Count < maxObjects) objectList.AddLast(obj);
            else result = false;

            return result;
        }

        public bool AddGameObject(Base3DGameObject obj, bool forceLoad)
        {
            bool result = AddGameObject(obj);

            if(forceLoad) obj.ForceLoadContent();

            return result;
        }

        #endregion
        }
}
